import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { ReqMiddleware } from '@game/req.middleware';
import { RouterModule } from 'nest-router-v2';
import { routes } from './routes';
import { JwtMiddleware } from '@game/jwt.middleware';
import { JwtModule } from '@nestjs/jwt';
import { defaultConfig } from '@game/config/config';
import { WebSocketGatewayModule } from './web-socket/web-socket.module';
import { RoomModule } from './web-socket/room/room.module';
import { SocketModule } from './web-socket/socket/socket.module';
import { CorsMiddleware } from './cors.middleware';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
@Module({
  imports: [
    JwtModule.register({
      secret: defaultConfig.webJwtConfig.secret,
      signOptions: { expiresIn: defaultConfig.webJwtConfig.expiresIn },
    }),
    RouterModule.forRoutes(routes), // setup the route
    RoomModule,
    SocketModule,
    WebSocketGatewayModule,AuthModule,UserModule
  ],
})
export class GameModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // 如果后续两个中间件需要单独放行，则一个改为守卫写法即可
    consumer
      .apply(ReqMiddleware, JwtMiddleware, CorsMiddleware)
      // .exclude({ path: '/user/a', method: RequestMethod.GET})
      .forRoutes({ path: 'api/*', method: RequestMethod.ALL });
  }
}
